package com.yiwei.ydd.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yiwei.ydd.fragment.MainIconFragment;

/**
 * Created by xsooy-pc on 17-12-5.
 */

public class MainHomePagerAdapter {
    private ViewPager pager;
    private List<String> data;
    private final int PageSize = 8;

    public MainHomePagerAdapter(ViewPager pager, FragmentManager fm, List<String> data) {
        this.pager = pager;
        this.data = data;
        init(fm);
    }

    private void init(FragmentManager fm) {
        int size = data.size();
        int count = size / PageSize + 1;
//        if (count == 0) {
//            count = 1;
//        }
        Fragment[] fragments = new Fragment[count];
        Map<String, List<String>> totalData = new HashMap<>();
        for (int i = 0; i < count; i++) {
            totalData.put("data" + i, new ArrayList<>());
        }


            int num = 0;
            for (int j = 0; j < data.size(); j++) {
                totalData.get("data" + num).add(data.get(j));
                if ((j + 1) % PageSize == 0) {
                    num++;
                }

            }

        for (int i = 0; i < count; i++) {
            MainIconFragment fragment = new MainIconFragment();
            fragment.setData(totalData.get("data" + i));
            fragments[i] = fragment;
        }


        pager.setAdapter(new FragmentPagerAdapter(fm) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
    }
}
